package com.laich.springcloud.service;

import java.util.List;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import com.laich.springcloud.dao.SysPermissionDao;
import com.laich.springcloud.dao.SysRolePermissionDao;
import com.laich.springcloud.dao.SysUserRoleDao;
import com.laich.springcloud.entity.SysPermission;
import com.laich.springcloud.entity.SysRolePermission;
import com.laich.springcloud.entity.SysUserRole;

/**
 * @author laich
 * @date 2019-02-12
 */
@Service
public class PermissionService {
	
	@Autowired
	private SysPermissionDao permissionDao;
	
	@Autowired
	private SysUserRoleDao sysUserRoleDao;
	
	@Autowired
	private SysRolePermissionDao sysRolePermissionDao;
	
	

    List<SysPermission> findByUserId(Integer userId){
    	 List<SysUserRole> sysUserRoleList = sysUserRoleDao.findByUserId(userId);
    	 if (CollectionUtils.isEmpty(sysUserRoleList)) {
             return null;
         }
         List<Integer> roleIdList = sysUserRoleList.stream().map(SysUserRole::getRoleId).collect(Collectors.toList());
         List<SysRolePermission> rolePermissionList = sysRolePermissionDao.findByRoleIds(roleIdList);
         if (CollectionUtils.isEmpty(rolePermissionList)) {
             return null;
         }
         List<Integer> permissionIdList = rolePermissionList.stream().map(SysRolePermission::getPermissionId).distinct().collect(Collectors.toList());
         List<SysPermission> sysPermissionList = permissionDao.findByIds(permissionIdList);
         if (CollectionUtils.isEmpty(sysPermissionList)) {
             return null;
         }
         return sysPermissionList;
    }

}
